package com.baymax.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 全局响应类
 *
 * @Author: baymax
 * @CreateTime: 2023/3/28 22:36
 * @Version: 1.0
 */
@Data
public class R<T> implements Serializable {

    /**
     * 成功
     */
    public final static int SUCCESS = 200;

    /**
     * 失败
     */
    public final static int FAIL = 500;

    private int code;

    private String msg;

    private T data;

    public static  <T> R<T> ok() {
        return restResult(null, SUCCESS, "操作成功！");
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, SUCCESS, "操作成功！");
    }

    public static <T> R<T> ok(String msg) {
        return restResult(null, SUCCESS, msg);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> R<T> fail() {
        return restResult(null, FAIL, "操作失败！");
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, FAIL, "操作失败！");
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static <T> R<T> fail(T data, int code, String msg) {
        return restResult(data, code, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> r = new R<>();
        r.setData(data);
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }
}
